﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using DAL;
using IDAL;
using System.Data.SqlClient;
using System.Configuration;

namespace DAL
{
   public class ShopingTables:IShoppingModel
    {
       public static string Conns = ConfigurationManager.ConnectionStrings["conn"].ConnectionString; 
       /// <summary>
       /// 添加到购物车
       /// </summary>
       /// <param name="shop"></param>
       /// <returns></returns>
        public int AddShopingCar(Model.ShoppingModel shop)
        {
            using(SqlConnection sql=new SqlConnection (Conns))
            {
                shop.Place_Time = DateTime.Now;
                string sqlstr = "insert into ShoppingTable(commodityID,FlowersName,PurchaseNumber,Sales_unit_price,Total_amount,Place_Time,UserID,FlowerUrl) values('"+shop.commodityID+"','"+shop.FlowersName+"',@Sums,"+shop.Sales_unit_price+",@Total_amount,'"+shop.Place_Time+"',"+shop.UserID+",'"+shop.FlowerUrl+"')";
                sql.Open();
                SqlCommand cmd = new SqlCommand(sqlstr,sql);
                cmd.Parameters.AddWithValue("@Sums", shop.purchaseNumber);
                cmd.Parameters.AddWithValue("@Total_amount", shop.purchaseNumber * shop.Sales_unit_price);
                return cmd.ExecuteNonQuery();
            }
        }
       /// <summary>
       /// 删除购物车信息
       /// </summary>
       /// <param name="shopid"></param>
       /// <returns></returns>
        public int removeShopingCar(string shopid)
        {
           using(SqlConnection sql=new SqlConnection (Conns))
           {
               string sqlstr = "delete ShoppingTable where ShopID in (" + shopid + ")";
               sql.Open();
               SqlCommand cmd = new SqlCommand(sqlstr, sql);
               return cmd.ExecuteNonQuery();
           }
        }
       /// <summary>
       /// 对购物车数量进行修改
       /// </summary>
       /// <param name="shopid"></param>
       /// <param name="sums"></param>
       /// <returns></returns>
        public int UpdateShoping(int shopid, int sums)
        {
            using (SqlConnection sql = new SqlConnection(Conns))
            {
                string sqlstr = "update ShoppingTable set purchaseNumber=" + sums + " where ShopID=" + shopid + "";
                sql.Open();
                SqlCommand cmd = new SqlCommand(sqlstr, sql);
                return cmd.ExecuteNonQuery();
            }
        }

       /// <summary>
       /// shopcar 显示
       /// </summary>
       /// <param name="UserID"></param>
       /// <returns></returns>
        public List<Model.ShoppingModel> getshop(int UserID)
        {
            using (SqlConnection sql = new SqlConnection(Conns))
            {
                string sqlstr = "select ShopID,commodityID,FlowersName,purchaseNumber,Sales_unit_price,Total_amount,Place_Time,FlowerUrl from ShoppingTable where UserID=" + UserID + "";
                sql.Open();
                SqlCommand cmd = new SqlCommand(sqlstr, sql);
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                DataTable ta = new DataTable();
                da.Fill(ta);
                List<Model.ShoppingModel> list = new List<Model.ShoppingModel>();
                foreach(DataRow i in ta.Rows)
                {
                    Model.ShoppingModel shop = new Model.ShoppingModel()
                    {
                        ShopID = Convert.ToInt32(i["ShopID"]),
                        commodityID = i["commodityID"].ToString(),
                        FlowersName = i["FlowersName"].ToString(),
                        purchaseNumber = Convert.ToInt32(i["purchaseNumber"]),
                        Sales_unit_price = Convert.ToInt32(i["Sales_unit_price"]),
                        Total_amount = Convert.ToInt32(i["Total_amount"]),
                        Place_Time = Convert.ToDateTime(i["Place_Time"]),
                        FlowerUrl=i["FlowerUrl"].ToString()

                    };
                    list.Add(shop);
                }
                return list;
            }
        }
    }
}
